package dominion.view;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import dominion.model.Card;
import dominion.model.Deck;
import dominion.model.GameBoard;
import dominion.model.Player;
import dominion.model.actions.Action;
import dominion.model.points.Gardens;
import dominion.utils.Constants;

public class Printer {

	private StringBuffer sb;

	public Printer() {
		sb = new StringBuffer();
	}

	public void print(String message) {
		sb.append(message);
		print();
	}

	private void print() {
		System.out.println(sb.toString());
		sb = new StringBuffer();
	}

	public void playerStatus(Player player) {
		sb.append("Player: \"" + player.getName() + "'s\" turn\n");
		sb.append("Actions: " + player.getActions() + "\n");
		sb.append("Money: " + player.getMoney() + "\n");
		sb.append("Buys: " + player.getBuys() + "\n");
		sb.append("Cards in hand:\n");
		sb.append(player.printHand());
		sb.append("\n");
		print();
	}

	public void printBoard(GameBoard board) {
		sb.append("(Cost)Kingdom Cards || Cards left\n");
		Map<String, Deck> map = board.getMap();
		List<Deck> deckList = new ArrayList<Deck>();
		for (String current : map.keySet())
		{
			Deck deck = map.get(current);
			boolean placed = false;
			if (deckList.isEmpty())
			{
				deckList.add(deck);
				placed = true;
			}
			for (Deck placedDeck : deckList)
			{
				if (deck.compareTo(placedDeck) < 0)
				{
					deckList.add(deckList.indexOf(placedDeck), deck);
					placed = true;
					break;
				}
			}
			if (!placed)
			{
				deckList.add(deck);
			}
		}

		for (Deck current : deckList)
		{
			if (!current.isEmpty()
					&& (current.revealTopCard() instanceof Action || current.revealTopCard() instanceof Gardens))
			{
				sb.append("(" + current.revealTopCard().getCost() + ")");
				sb.append(current.revealTopCard().toString());
				sb.append(" || ");
				sb.append(current.size());
				sb.append("\n");
			}
		}

		sb.append("\n");
		if (!board.getEstates().isEmpty())
		{
			sb.append("Estates || " + board.getEstates().size() + "\n");
		}
		if (!board.getDuchies().isEmpty())
		{
			sb.append("Duchies || " + board.getDuchies().size() + "\n");
		}
		if (!board.getProvinces().isEmpty())
		{
			sb.append("Provinces || " + board.getProvinces().size() + "\n");
		}

		print();
	}

	public void printOptions() {
		// sb.append("Type \"help {card name}\" for a card description\n");
		// sb.append("Type \"{card name}\" to play card\n");
		// sb.append("Type \"buy {card name}\" to buy a card\n");
		// sb.append("Player options: \n");
		// sb.append(Constants.PLAYER_STATUS + "\n");
		// sb.append(Constants.BOARD_STATUS + "\n");
		// sb.append(Constants.END_TURN + "\n");
		// print();
	}

	public void printCardHelp(String card) {
		Card temp = Constants.cardMap.get(card.toLowerCase());
		if (temp == null)
		{
			sb.append("Invalid Card Name given\n");
		}
		else
		{
			sb.append(temp.getDescription());
		}
		print();
	}

	public void printWinners(List<String> winners) {
		if (winners.size() == 1)
		{
			sb.append("The winner is: \n");
			sb.append(winners.get(0) + "!!\n");
		}
		else if (winners.isEmpty())
		{
			sb.append("nobody won");
		}
		else
		{
			sb.append("There is a tie. Winners are: \n");
			for (String current : winners)
			{
				sb.append(current + "!!\n");
			}
		}
		print();
	}
}
